public class LinkedList implements List{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head = null;
    public ListNode tail = null;

    public int usedSize = 0;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(isEmpty()) {
            head = tail = node;
        }else {
            head.prev = node;
            node.next = head;
            head = node;
        }
        usedSize++;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(isEmpty()) {
            head = tail = node;
        }else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
        usedSize++;
    }

    @Override
    public boolean addIndex(int index, int data) {
        if(index < 0 || index > usedSize) return false;
        ListNode node = new ListNode(data);
        if(isEmpty()) {
            head = tail = node;
        }
        if(index == 0) {
            head.prev = node;
            node.next = head;
            head = node;
        } else if (index == usedSize) {
            tail.next = node;
            node.prev = tail;
            tail = node;
        } else {
            ListNode cur = head;
          while (index != 0) {
              cur = cur.next;
              index--;
          }
          node.prev = cur.prev;
          node.next = cur;
          cur.prev.next = node;
          cur.prev = node;
        }
        usedSize++;
        return true;
    }

    @Override
    public boolean contains(int key) {
        if(isEmpty()) {
            return false;
        }
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(isEmpty()) {
            return;
        }
        if(head.val == key) {
            head.next.prev = null;
            head = head.next;
            usedSize--;
            return;
        }
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                if(cur.next != null) {
                    cur.next.prev = cur.prev;
                }
                usedSize--;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(isEmpty()) {
            return;
        }
        while(head != null&&head.val == key) {
            if(head.next != null) {
                head.next.prev = null;
            }
            head = head.next;
            usedSize--;
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                if(cur.next != null) {
                    cur.next.prev = cur.prev;
                }
                usedSize--;
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void display() {
        if(isEmpty()) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    private boolean isEmpty() {
        return head == null;
    }

    @Override
    public void clear() {
        head = null;
        usedSize = 0;
    }
}
